/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server;

import java.util.logging.Logger;

import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.gametime.L1GameTimeClock;

public class ShipTimeController implements Runnable {
	private static Logger _log = Logger.getLogger(ShipTimeController.class
			.getName());

	private static ShipTimeController _instance;

	public static ShipTimeController getInstance() {
		if (_instance == null) {
			_instance = new ShipTimeController();
		}
		return _instance;
	}

	@Override
	public void run() {
		try {
			while (true) {
				checkShipTime(); // 船の到着時間をチェック
				Thread.sleep(5000);
			}
		} catch (Exception e1) {
		}
	}

	private void checkShipTime() {
		int servertime = L1GameTimeClock.getInstance().getGameTime()
				.getSeconds();
		int nowtime = servertime % 86400;
		if (nowtime >= 7 * 3600
				&& nowtime < 8 * 3600 // FI行きの船
				|| nowtime >= 11 * 3600 && nowtime < 12 * 3600
				|| nowtime >= 15 * 3600 && nowtime < 16 * 3600
				|| nowtime >= 19 * 3600 && nowtime < 20 * 3600) {
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId() == 83) {
					L1Teleport.teleport(pc, 32936, 33057, (short) 70, 0, false);
					pc.getInventory().removeItem(40300, 1); // 忘れられた島行きチケット
				}
			}
		}
		if (nowtime >= 9 * 3600
				&& nowtime < 10 * 3600 // ハイネ行きの船
				|| nowtime >= 13 * 3600 && nowtime < 14 * 3600
				|| nowtime >= 17 * 3600 && nowtime < 18 * 3600
				|| nowtime >= 21 * 3600 && nowtime < 22 * 3600) {
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId() == 84) {
					L1Teleport.teleport(pc, 32936, 33057, (short) 70, 0, false);
					pc.getInventory().removeItem(40301, 1); // ハイネ行きチケット
				}
			}
		}
		if (nowtime >= 9 * 3600
				&& nowtime < 10 * 3600 // 海賊島行きの船
				|| nowtime >= 13 * 3600 && nowtime < 14 * 3600
				|| nowtime >= 17 * 3600 && nowtime < 18 * 3600
				|| nowtime >= 21 * 3600 && nowtime < 22 * 3600) {
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId() == 447) {
					L1Teleport
							.teleport(pc, 32297, 33087, (short) 440, 0, false);
					pc.getInventory().removeItem(40302, 1); // 海賊島行きチケット
				}
			}
		}
		if (nowtime >= 7 * 3600
				&& nowtime < 8 * 3600 // 隠された船着場行きの船
				|| nowtime >= 11 * 3600 && nowtime < 12 * 3600
				|| nowtime >= 15 * 3600 && nowtime < 16 * 3600
				|| nowtime >= 19 * 3600 && nowtime < 20 * 3600) {
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId() == 446) {
					L1Teleport
							.teleport(pc, 32750, 32874, (short) 445, 0, false);
					pc.getInventory().removeItem(40303, 1); // 隠された船着場行きチケット
				}
			}
		}
	}
}
